package com.cn.sl.server.api.system.user.model.entity

import com.cn.sl.server.api.system.department.model.entity.DepartmentEntity
import com.cn.sl.server.api.system.file.model.entity.SysFileEntity
import com.cn.sl.server.api.system.role.model.entity.RoleEntity
import com.cn.sl.server.common.model.SoftDeleteEditableEntity
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.FetchType
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToMany
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import org.hibernate.annotations.NotFound
import org.hibernate.annotations.NotFoundAction

@Table(name = "admin_user")
@Entity
class AdminUserEntity : SoftDeleteEditableEntity<AdminUserEntity>() {
    @Column(name = "avatar_id")
    var avatarId: Long? = null

    @NotFound(action = NotFoundAction.IGNORE)
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(
        name = "avatar_id",
        referencedColumnName = "id",
        insertable = false,
        updatable = false
    )
    var avatar: SysFileEntity? = null

    /**
     * 用户名
     */
    var username: String = ""

    /**
     * 联系电话
     */
    var phone: String = ""

    /**
     * 密码
     */
    @Column(updatable = false)
    var password: String = ""

    /**
     * 盐
     */
    @Column(updatable = false)
    var salt: String = ""

    /**
     * 用户首页id
     */
    var homePageId: Long? = null

    @ManyToMany(fetch = FetchType.EAGER, mappedBy = "users", cascade = [CascadeType.PERSIST])
    var roles: List<RoleEntity> = listOf()

    @Enumerated(EnumType.STRING)
    var status: UserStatus = UserStatus.ENABLE

    @Column(name = "department_id")
    var departmentId: Long? = null

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "department_id", insertable = false, updatable = false)
    var department: DepartmentEntity? = null

}